/*****
 * BEGIN LICENSE BLOCK ***** Version: MPL 1.1
 * 
 * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
 * either express or implied. See the License for the specific language governing rights and limitations under
 * the License.
 * 
 * The Original Code is BzStats.
 * 
 * The Initial Developer of the Original Code is Michel de Bree (youth). Portions created by the Initial
 * Developer are Copyright (C) 2005 the Initial Developer. All Rights Reserved.
 * 
 * Contributor(s):
 * 
 ***** END LICENSE BLOCK
 *****/

package bzstats;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;

import org.apache.log4j.Logger;

import bzstats.event.GameEvent;
import bzstats.event.KillEvent;
import bzstats.event.ParticipateEvent;
import bzstats.event.SuicideEvent;
import bzstats.event.WinEvent;

/**
 * @author youth
 * 
 */
public class LogReader extends BufferedReader {

	private final static Logger LOG = Logger.getLogger(LogReader.class);

	/**
	 * @param arg0
	 */
	public LogReader(Reader arg0) {
		super(arg0);
	}

	public GameEvent nextEvent() throws IOException, BzStatsException {

		String logline;
		GameEvent event = null;

		boolean eof = false;

		while ((event == null) && !eof) {

			logline = readLine();
			eof = (logline == null);

			if (!eof) {
				event = getEvent(logline);
			}

		}

		return event;

	}

	/**
	 * Parses a logline and returns a GameEvent if found, else <code>null</code>
	 * 
	 * @param logline One line from the BzFlag logfile
	 * @return A GameEvent, or <code>null</code> if no GameEvent found.
	 * @throws BzStatsException
	 */
	private static GameEvent getEvent(final String logline) throws BzStatsException {

		final GameEvent killevent = new KillEvent();
		final GameEvent suicideevent = new SuicideEvent();
		final GameEvent winevent = new WinEvent();
		final GameEvent participateevent = new ParticipateEvent();

		GameEvent result = null;

		if (killevent.parseEvent(logline)) {
			result = killevent;
		} else if (suicideevent.parseEvent(logline)) {
			result = suicideevent;
		} else if (winevent.parseEvent(logline)) {
			result = winevent;
		} else if (participateevent.parseEvent(logline)) {
            result = participateevent;
        }

		return result;

	}

}